<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;        
        default:
            $url = U($url);
            break;
    }
    return $url;
}
/*
     * 格式化时间戳
     * @param timestamp string  时间戳
     * @param type int 返回类型
     */
    function TimeFormat($timestamp,$type=0){
        $return = "";
        $time = time();
        if($type === 0){
            $different = $time-$timestamp;
            if($different > 259200 ) $return = date("Y年m月d日",$timestamp);
            else if($different > 86400) $return = intval($different/86400) . "天以前";
            else if($different > 3600) $return = intval($different/3600) . "小时以前";
            else if($different > 60) $return = intval($different/60) . "分钟以前";
            else $return = $different . "秒钟以前";
        }else if($type === -1){
            $different = $timestamp-$time;
            if($different <0) $return = "已";
            else if($different > 259200 ) $return = date("Y年m月d日",$timestamp);
            else if($different > 86400) $return = intval($different/86400) . "天之后";
            else if($different > 3600) $return = intval($different/3600) . "小时之后";
            else if($different > 60) $return = intval($different/60) . "分钟之后";
            else $return = $different . "秒钟之后";
        }else if($type == "1"){
            $return = $return = date("Y-m-d h:i:s",$timestamp);
        }else{
            $return = $return = date($type,$timestamp);
        }
        return $return;
    }